package com.recycle.system.service.impl;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.recycle.common.enums.Recyclable;
import com.recycle.common.enums.ReferenceTypePrice;
import com.recycle.common.utils.DateUtils;
import com.recycle.common.utils.SecurityUtils;
import com.recycle.common.utils.StringUtils;
import com.recycle.system.domain.WasteResourceType;
import com.recycle.system.domain.dto.ReferPriceDTO;
import com.recycle.system.mapper.WasteResourceTypeMapper;
import jdk.management.resource.ResourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.recycle.system.mapper.WasteResourceMapper;
import com.recycle.system.domain.WasteResource;
import com.recycle.system.service.IWasteResourceService;

import javax.annotation.Resource;

/**
 * 再生资源Service业务层处理
 *
 * @author recycle
 * @date 2023-03-11
 */
@Service
public class WasteResourceServiceImpl implements IWasteResourceService
{
    @Autowired
    private WasteResourceMapper wasteResourceMapper;


    @Resource
    private WasteResourceTypeMapper wasteResourceTypeMapper;
    /**
     * 查询再生资源
     *
     * @param resourceId 再生资源主键
     * @return 再生资源
     */
    @Override
    public WasteResource selectWasteResourceByResourceId(Long resourceId)
    {
        return wasteResourceMapper.selectWasteResourceByResourceId(resourceId);
    }

    /**
     * 查询再生资源列表
     *
     * @param wasteResource 再生资源
     * @return 再生资源
     */
    @Override
    public List<WasteResource> selectWasteResourceList(WasteResource wasteResource)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            wasteResource.setOwnerId(SecurityUtils.getUserId());
        }
        List<WasteResource> wasteResources = wasteResourceMapper.selectWasteResourceList(wasteResource);
        wasteResources.stream().forEach(item -> {
            if(item.getPrice()==null) {
                //没有价格则默认设置参考价格
                item.setPrice(new BigDecimal(ReferenceTypePrice.findPrice(item.getResourceTypeId())));
            }
        });
        return wasteResources;
    }

    /**
     * 模糊查询再生资源列表
     *
     * @param wasteResource 再生资源
     * @return 再生资源集合
     */
    @Override
    public List<WasteResource> fuzzySelectWasteResourceList(WasteResource wasteResource) {
        QueryWrapper wrapper = new QueryWrapper<WasteResource>();
        Arrays.stream(WasteResource.class.getDeclaredFields()).forEach(field -> {
           field.setAccessible(true);
           if(field.getType() != String.class){
               return;
           }
            try {
                String value =(String)field.get(wasteResource);
                if(StringUtils.isNotEmpty(value)){
                    wrapper.like(field.getName(),value);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        });
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            wrapper.eq("owner_id",SecurityUtils.getUserId());
        }
        return wasteResourceMapper.selectList(wrapper);
    }

    /**
     * 新增再生资源
     *
     * @param wasteResource 再生资源
     * @return 结果
     */
    @Override
    public int insertWasteResource(WasteResource wasteResource)
    {
        wasteResource.setCreateTime(DateUtils.getNowDate());
        wasteResource.setResourceTypeId(Recyclable.findValue(wasteResource.getResourceType()));
        return wasteResourceMapper.insertWasteResource(wasteResource);
    }

    /**
     * 修改再生资源
     *
     * @param wasteResource 再生资源
     * @return 结果
     */
    @Override
    public int updateWasteResource(WasteResource wasteResource)
    {
        wasteResource.setUpdateTime(DateUtils.getNowDate());
        return wasteResourceMapper.updateWasteResource(wasteResource);
    }

    /**
     * 批量删除再生资源
     *
     * @param resourceIds 需要删除的再生资源主键
     * @return 结果
     */
    @Override
    public int deleteWasteResourceByResourceIds(Long[] resourceIds)
    {
        return wasteResourceMapper.deleteWasteResourceByResourceIds(resourceIds);
    }

    /**
     * 删除再生资源信息
     *
     * @param resourceId 再生资源主键
     * @return 结果
     */
    @Override
    public int deleteWasteResourceByResourceId(Long resourceId)
    {
        return wasteResourceMapper.deleteWasteResourceByResourceId(resourceId);
    }

    /**
     * 查询参考价格列表
     *
     * @return 结果
     */
    @Override
    public List<ReferPriceDTO> selectReferPrice() {
        List<ReferPriceDTO> list = wasteResourceTypeMapper.selectReferPrice();
        list.stream().forEach(item -> {
            item.typeTransformer();
        });
        return list;
    }
}
